package com.yc.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.yc.vo.Result;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.yc.entity.TeacherDelays;
import com.yc.mapper.TeacherDelaysMapper;
import com.yc.service.TeacherDelaysService;

import java.util.List;
import java.util.Map;

/**
 * 老师上课迟到表;(teacher_delays)表服务实现类
 * @date : 2023-6-28
 */
@Service
public class TeacherDelaysServiceImpl implements TeacherDelaysService{
    @Autowired
    private TeacherDelaysMapper teacherDelaysMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public TeacherDelays queryById(Integer id){
        return teacherDelaysMapper.selectById(id);
    }

    /**
     * 根据classID 和 课程名 查出老师Id
     * @param classId
     * @param
     * @return
     */
    public Integer queryTeaID(int classId,String classtime,String date){
        Integer teacherId = teacherDelaysMapper.queryTeacher(classId, classtime,date);
        return teacherId;
    }

    /**
     * 查询为处理消息数量
     * @return
     */
    public int queryCount(String stuid){
        int count = teacherDelaysMapper.queryCount(stuid);
        return count;
    }

    /**
     * 班长查举报信息
     * @param stuid
     * @return
     */
    public List<Map<String,Object>> queryInfoTea(String stuid){
        List<Map<String, Object>> list = teacherDelaysMapper.queryInfoTea(stuid);
        return list;
    }

    /**
     * 领导查看已经提交的举报信息
     * @return
     */
    public List<Map<String,Object>> managerquery(int page,String date){
        PageHelper.startPage(page,2);
        List<Map<String, Object>> list = teacherDelaysMapper.managerquery(date);
        return list;
    }

    /**
     * 添加老师迟到数据
     * @param teacherId
     * @param scheduleId
     * @param delaydate
     * @param stuid
     * @param remarks
     * @return
     */
    public Result insertTeaDelays(int teacherId,int scheduleId,String delaydate,String stuid,int cid,int status,String remarks){
        teacherDelaysMapper.insertTeacher(teacherId,scheduleId,delaydate,stuid,cid,status,remarks);
        return Result.success("提交成功");
    }

    /**
     * 查询已上交老师迟到数据总数
     * @param date
     * @return
     */
    public int queryTotal(String date){
        int total = teacherDelaysMapper.queryTotal(date);
        return total;
    }

    /**
     * 班长提交举报老师迟到
     * @param id
     * @return
     */
    public Result updateStatus(int id){
        teacherDelaysMapper.updateStatus(id);

        return Result.success("已处理");
    }

    /**
     * 班长提交举报老师迟到
     * @param id
     * @return
     */
    public Result updateStatusFalse(int id){
        teacherDelaysMapper.updateStatusFale(id);
        return Result.success("已驳回");
    }


    /**
     * 分页查询
     *
     * @param teacherDelays 筛选条件
     * @param current 当前页码
     * @param size  每页大小
     * @return
     */
    @Override
    public Page<TeacherDelays> paginQuery(TeacherDelays teacherDelays, long current, long size){
        //1. 构建动态查询条件
        LambdaQueryWrapper<TeacherDelays> queryWrapper = new LambdaQueryWrapper<>();
        if(StrUtil.isNotBlank(teacherDelays.getStuid())){
            queryWrapper.eq(TeacherDelays::getStuid, teacherDelays.getStuid());
        }
        if(StrUtil.isNotBlank(teacherDelays.getRemarks())){
            queryWrapper.eq(TeacherDelays::getRemarks, teacherDelays.getRemarks());
        }
        //2. 执行分页查询
        Page<TeacherDelays> pagin = new Page<>(current , size , true);
        IPage<TeacherDelays> selectResult = teacherDelaysMapper.selectByPage(pagin , queryWrapper);
        pagin.setPages(selectResult.getPages());
        pagin.setTotal(selectResult.getTotal());
        pagin.setRecords(selectResult.getRecords());
        //3. 返回结果
        return pagin;
    }

    /**
     * 新增数据
     *
     * @param teacherDelays 实例对象
     * @return 实例对象
     */
    @Override
    public TeacherDelays insert(TeacherDelays teacherDelays){
        teacherDelaysMapper.insert(teacherDelays);
        return teacherDelays;
    }

    /**
     * 更新数据
     *
     * @param teacherDelays 实例对象
     * @return 实例对象
     */
    @Override
    public TeacherDelays update(TeacherDelays teacherDelays){
        //1. 根据条件动态更新
        LambdaUpdateChainWrapper<TeacherDelays> chainWrapper = new LambdaUpdateChainWrapper<TeacherDelays>(teacherDelaysMapper);
        if(StrUtil.isNotBlank(teacherDelays.getStuid())){
            chainWrapper.eq(TeacherDelays::getStuid, teacherDelays.getStuid());
        }
        if(StrUtil.isNotBlank(teacherDelays.getRemarks())){
            chainWrapper.eq(TeacherDelays::getRemarks, teacherDelays.getRemarks());
        }
        //2. 设置主键，并更新
        chainWrapper.set(TeacherDelays::getId, teacherDelays.getId());
        boolean ret = chainWrapper.update();
        //3. 更新成功了，查询最最对象返回
        if(ret){
            return queryById(teacherDelays.getId());
        }else{
            return teacherDelays;
        }
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id){
        int total = teacherDelaysMapper.deleteById(id);
        return total > 0;
    }
}
